package spider.manipulation;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.log4j.Logger;
import org.apache.log4j.Priority;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.billgaty.searchapi.Page;
import com.billgaty.searchapi.SearchEngine;

import spider.daos.ResultManipulationEntityManager;
import spider.entities.KeyWords;
import spider.entities.Result;
import spider.entities.ResultContent;
import spider.entities.TreatedResult;
 
public class Main {

	public static void main(String[] args) { 
		// TODO Auto-generated method stub
		String applicationfile="application.xml";
			 
ApplicationContext ctx=new   //FileSystem 
ClassPathXmlApplicationContext (applicationfile);
Logger logger=Logger.getLogger(Main.class);
logger.log(Priority.INFO, "loading classpath");
spider.daos.ResultManipulationEntityManager em = ctx.getBean( ResultManipulationEntityManager.class);
System.out.println(em);
String keyword[]=new String[]{"statut matrimonial de flora hollande","Flora Hollande","actualité de la bourse","les plans secrets","la geurre du golf"};
//System.out.println(em.getTemplate());
  rechercher(keyword,em);
 recherchercontenu(em);
traiterrecherche(em);
List<Object> l = em.query("from TreatedResult", null);
for (Object object : l) {
	System.out.println(((TreatedResult)object).getRelatedresult());
}

	}
	public static  void traiterrecherche( spider.daos.ResultManipulationEntityManager em ){
		List<Object> l = em.query("from ResultContent", null);
		for (Object object : l) {
			ResultContent rc=(ResultContent) object;
			String content=rc.getContent();
			TreatedResult tr=new TreatedResult();
			tr.setRelatedresult(rc);
			ArrayList<String> ar=new ArrayList<String>();
			int i=content.indexOf(rc.getResult().getQuery().getQuery()),i1,i2;
			int j=content.lastIndexOf(rc.getResult().getQuery().getQuery()),j1,j2;
			if(i-50>0)i1=i-50;
			else
				i1=0;
			if(i+50<content.length())
				i2=i+50;
			else
				i2=content.length();
			if(j-50>0)j1=j-50;
			else
				j1=0;
			if(j+50<content.length())
				j2=j+50;
			else
				j2=content.length();
				
			ar.add(content.substring(j1 , j2));
			tr.setUsefullcontent(ar);
			System.out.println(tr);
			em.persist(tr);
			
		}
	}
	public static  void recherchercontenu( spider.daos.ResultManipulationEntityManager em ){
		ArrayList  keys=(ArrayList ) em.query("from KeyWords ", null);
		for (Object object : keys) {
			 KeyWords kw=(KeyWords) object;
			 String HQL="select new Result(r.id, r.url, r.title, r.content,"
			+"r.visibleUrl,r.gsearchResultClass, r.unescapedUrl,"
			+"r.cacheUrl, r.titleNoFormatting,kw)  from Result r ,KeyWords kw  where r.query.query=kw.query and kw.query=?";
			 ArrayList  liste=(ArrayList) em.query(HQL, kw.getQuery());
			 for (Object object2 : liste) {
				Result r=(Result) object2;
				String content=loadcontent(r);
				ResultContent rc=new ResultContent();
				rc.setContent(content);
				rc.setResult(r);
				em.persist(rc);
				 
				// System.out.println(rc);
			}
		}
	}
		
		
	private static String loadcontent(Result r) {
		// TODO Auto-generated method stub
		HttpClient cl=new DefaultHttpClient();
		try {
			HttpGet get=new HttpGet(new URI(r.getUrl()));
		HttpResponse ht = cl.execute(get);
		BufferedReader lecture= new BufferedReader(new InputStreamReader(ht.getEntity().getContent()));
		StringBuilder result=new StringBuilder();
		String tmp=null;
		while(null!=(tmp=lecture.readLine()))
			result.append(tmp);
		return result.toString();
		} catch (URISyntaxException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	public static  void rechercher(String[] keyword,spider.daos.ResultManipulationEntityManager em ){
		for (String string : keyword) {
			 KeyWords key=new KeyWords(string);
			 System.out.println(key);
			 em.persist(key);
			 // em.getTemplate().load(Result.class, 1L);
			 List<Page> pages=null;
			 SearchEngine se=null;
			 try{
			   se=(new SearchEngine()).search(string);
			 
			  pages=se.getResponseData().getCursor().getPages();
			 }catch(Exception e){
				 em.delete(key);
				 continue;
				
			 }
			 
			 for (Page page : pages) {
				se.setStart(page.getStart());
				SearchEngine re=se.search();
				System.out.println(re);
				 if(re.isOk()){
					 List<com.billgaty.searchapi.Result> l = re.getResponseData().getResults();
					 System.out.println(l);
					 for (com.billgaty.searchapi.Result result : l) {
						Result r=new Result();
						r.setUrl(result.getUrl());
						r.setCacheUrl(result.getCacheUrl());
						r.setVisibleUrl(result.getVisibleUrl());
						r.setGsearchResultClass(result.getGsearchResultClass());
						r.setTitle(result.getTitle());
						r.setUnescapedUrl(result.getUnescapedUrl());
						r.setQuery(key);
						r.setContent(result.getContent());
						r.setTitleNoFormatting(result.getTitleNoFormatting());
						em.persist(r);
					}
				 }
				
			}

		}

	}

}
